#include "TwApplicationPCH.h"
#include "TwMeshManager.h"

TwImplementSingleton(TwMeshManager);

TwMeshManager::TwMeshManager()
{

}

TwMeshManager::~TwMeshManager()
{
	MeshIterator i;
	for (i = mMeshList.begin(); i != mMeshList.end(); ++i)
	{
		TwSafeDelete(i->second);
	}
	mMeshList.clear();
}

TwMesh* TwMeshManager::createMesh(LPCSTR filename)
{
	TwMesh* mesh = getMesh(filename);
	if (mesh == NULL)
	{
		mesh = TwNew TwMesh;
		if (!mesh->load(filename))
		{
			TwSafeDelete(mesh);
		}
		else
			mMeshList.insert(MeshValue(mesh->getName(), mesh));
	}

	return mesh;
}

TwMesh* TwMeshManager::getMesh(const std::string& name)
{
	MeshIterator i = mMeshList.find(name);
	if (i != mMeshList.end())
		return i->second;
	else
		return NULL;
}